<?php

namespace App\Master\PUT;

/**
 * 提交内容
 */
class Article extends \App\Master\CheckUser {

    /**
     * 更新日志标题
     */
    public function action() {
        $data['noset']['timelog_id'] = $this->isP('id', '请选择更新的日志');
        $data['timelog_title'] = $this->isP('title', '请提交更新的标题');

        if ($_SESSION['user']['user_group_id'] == 1) {
            $data['timelog_type'] = $this->isP('timelog_type', '请选择类型');
        }

        $checkUser = $this->db('timelog')->where('timelog_id = :timelog_id AND user_id = :user_id')->find([
            'timelog_id' => $data['noset']['timelog_id'],
            'user_id' => $_SESSION['user']['user_id']
        ]);

        if(empty($checkUser) && $_SESSION['user']['user_group_id'] != 1){
            $this->error('您没有权限操作本日志');
        }

        $this->db('timelog')->where('timelog_id = :timelog_id')->update($data);

        $this->success('更新标题成功!', $this->url('Master-Article-view', ['id' => $data['noset']['timelog_id']]));
    }

    /**
     * 更新内容
     */
    public function updateContent() {
        $id = $this->isG('id', '请提交您要编辑的内容');
        $content = $this->isP('content', '请填写内容');
        $checkUser = $this->db('timelog_content')->where('timelog_content_id = :timelog_content_id AND user_id = :user_id ')->find(array(
                'timelog_content_id' => $id,
                'user_id' => $_SESSION['user']['user_id']
        ));
        if (empty($checkUser)) {
            $this->error('没有找到您要更新的内容');
        }

        $update = $this->db('timelog_content')->where('timelog_content_id = :timelog_content_id AND user_id = :user_id ')->update(array(
            'timelog_content' => $content,
            'timelog_content_updatetime' => time(),
            'noset' => array(
                'timelog_content_id' => $id,
                'user_id' => $_SESSION['user']['user_id']
            )
        ));
        if ($update === false) {
            $this->error('更新出错');
        }

        $this->success('更新成功', $this->url('Master-Article-view', ['id' => $checkUser['timelog_id']]));
    }

}
